class Solution
{
public:
    int stockManagement(vector<int>& stock)
    {
        int l = 0, r = stock.size() - 1;
        while (l < r)
        {
            int m = (l + r) / 2;
            if (stock[m] > stock[r])
                l = m + 1;
            else if (stock[m] < stock[r])
                r = m;
            else
            {
                int x = l;
                for (int k = l + 1; k < r; k++)
                    if (stock[k] < stock[x])
                        x = k;
                return stock[x];
            }
        }
        return stock[l];
    }
};